package com.xinxin.facelinker.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.adapter.ToplistAdapter;
import com.xinxin.facelinker.domain.ShowToplist;
import com.xinxin.facelinker.ui.button.views.ButtonFloat;
import com.xinxin.facelinker.ui.grid.StaggeredGridView;
import com.xinxin.facelinker.ui.menu.ContextMenuDialogFragment;
import com.xinxin.facelinker.ui.menu.MenuObject;
import com.xinxin.facelinker.ui.menu.MenuParams;
import com.xinxin.facelinker.ui.menu.interfaces.OnMenuItemClickListener;
import com.xinxin.facelinker.ui.menu.interfaces.OnMenuItemLongClickListener;

import java.util.ArrayList;
import java.util.List;

public class ShowFemaleToplistActivity extends FragmentActivity implements OnMenuItemClickListener,
        OnMenuItemLongClickListener {

    private StaggeredGridView lvShowFemaleToplist;
    private HttpUtils httpUtils = new HttpUtils();
    private ArrayList<ShowToplist> list = new ArrayList<ShowToplist>();
    private View btnBack;
    private ButtonFloat btnMenu;
    private FragmentManager fragmentManager = getSupportFragmentManager();
    private DialogFragment mMenuDialogFragment;
    private ToplistAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_female_toplist);

        lvShowFemaleToplist = (StaggeredGridView) findViewById(R.id.lvShowFemaleToplist);
        btnBack = findViewById(R.id.btnBack);

        btnMenu = (ButtonFloat) findViewById(R.id.btnMenu);

        initMenuFragment();

        btnMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMenuDialogFragment.onAttach(ShowFemaleToplistActivity.this);
                fragmentManager.beginTransaction().add(mMenuDialogFragment, "").commit();
            }
        });

        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_SHOW_FEMALE_TOPLIST);
        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                list = gson.fromJson(responseInfo.result, new TypeToken<ArrayList<ShowToplist>>() {
                }.getType());

                adapter = new ToplistAdapter(ShowFemaleToplistActivity.this, list);
                lvShowFemaleToplist.setAdapter(adapter);

                lvShowFemaleToplist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent=new Intent(ShowFemaleToplistActivity.this,GetPalInfoActivity.class);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(ShowFemaleToplistActivity.this, R.string.show_female_toplist_fail, Toast.LENGTH_SHORT).show();
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initMenuFragment() {
        MenuParams menuParams = new MenuParams();
        menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
        menuParams.setMenuObjects(getMenuObjects());
        menuParams.setClosableOutside(false);
        mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    }

    private List<MenuObject> getMenuObjects() {

        List<MenuObject> menuObjects = new ArrayList<>();

        MenuObject close = new MenuObject();
        close.setResource(R.drawable.btn_close);

        MenuObject pals_motion = new MenuObject("朋友圈");
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_pals_motion);
        pals_motion.setBitmap(b);

        MenuObject my_motion = new MenuObject("自己动态");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_my_motion);
        my_motion.setBitmap(b);

        MenuObject near_pal = new MenuObject("附近的人");
        BitmapDrawable bd = new BitmapDrawable(getResources(),
                BitmapFactory.decodeResource(getResources(), R.drawable.btn_near_pal));
        near_pal.setDrawable(bd);

        MenuObject blacklist = new MenuObject("黑名单");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_blacklist);
        blacklist.setBitmap(b);

        MenuObject male_toplist = new MenuObject("男神榜");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_male_toplist);
        male_toplist.setBitmap(b);

        menuObjects.add(close);
        menuObjects.add(pals_motion);
        menuObjects.add(my_motion);
        menuObjects.add(near_pal);
        menuObjects.add(male_toplist);
        menuObjects.add(blacklist);

        return menuObjects;
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_image:
                if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                    mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
            mMenuDialogFragment.dismiss();
        } else {
            finish();
        }
    }

    public void onMenuItemClick(View clickedView, int position) {
        if (position == 1) {
            Intent intent = new Intent(ShowFemaleToplistActivity.this, MainActivity.class);
            startActivity(intent);
            finish();

        } else if (position == 2) {
            Intent intent = new Intent(ShowFemaleToplistActivity.this, ShowMyMotionActivity.class);
            startActivity(intent);
            finish();

        } else if (position == 3) {
            Intent intent = new Intent(ShowFemaleToplistActivity.this, ShowNearUserMotionActivity.class);
            startActivity(intent);
            finish();

        } else if (position == 4) {
            Intent intent = new Intent(ShowFemaleToplistActivity.this, ShowMaleToplistActivity.class);
            startActivity(intent);
            finish();

        } else if (position == 5) {
            Intent intent = new Intent(ShowFemaleToplistActivity.this, ShowBlacklistActivity.class);
            startActivity(intent);
            finish();

        }
    }

    public void onMenuItemLongClick(View clickedView, int position) {
    }
}
